﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

namespace CodeCity.AutomationMachine.DomainModels
{
    public class ModelBase
    {
        public TKey WithReadLock<TModel, TKey>(ReaderWriterLockSlim lockSlim, Func<TModel, TKey> propertyAccessor)
            where TModel : ModelBase
        {
            try
            {
                lockSlim.EnterReadLock();
                return propertyAccessor((TModel)this);
            }
            finally
            {
                lockSlim.ExitReadLock();
            }
        }
        public void WithWriteLock<TModel>(ReaderWriterLockSlim lockSlim, Action<TModel> propertyAccessor)
            where TModel : ModelBase
        {
            try
            {
                lockSlim.EnterWriteLock();
                propertyAccessor((TModel)this);
            }
            finally
            {
                lockSlim.ExitWriteLock();
            }
        }
    }
}
